package com.jian.bus.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jian.bus.pojo.Provider;
import com.jian.bus.service.ProviderService;
import com.jian.bus.vo.ProviderVo;
import com.jian.sys.common.Constant;
import com.jian.sys.common.DataGridView;
import com.jian.sys.common.ResultObj;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 剑
 * @since 2020-01-20
 */
@RestController
@RequestMapping("/provider")
public class ProviderController {
    @Autowired
    ProviderService providerService;
    /**
     * 查询所有和模糊查询
     */
    @RequestMapping("/loadAllProvider")
    public DataGridView loadAllProvider(ProviderVo providerVo){

        IPage<Provider> page = new Page<>(providerVo.getPage(),providerVo.getLimit());
        QueryWrapper<Provider> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(providerVo.getProvidername()),"providername",providerVo.getProvidername());
        wrapper.like(StringUtils.isNotBlank(providerVo.getConnectionperson()),"connectionperson",providerVo.getConnectionperson());
        wrapper.like(StringUtils.isNotBlank(providerVo.getPhone()),"phone",providerVo.getPhone());
        this.providerService.page(page,wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 添加供应商
     * @param providerVo
     * @return
     */
    @RequestMapping("/addProvider")
    public ResultObj addProvider(ProviderVo providerVo){
        try{
            this.providerService.save(providerVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }
    /**
     * 修改供应商
     * @param providerVo
     * @return
     */
    @RequestMapping("/updateProvider")
    public ResultObj updateProvider(ProviderVo providerVo){
        try{
            this.providerService.updateById(providerVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除供应商
     * @param id
     * @return
     */
    @RequestMapping("/deleteProvider")
    public ResultObj deleteProvider(Integer id){
        try {
            this.providerService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("/batchDeleteProvider")
    public ResultObj batchDeleteProvider(Integer[] ids){
        try {
            Collection<Serializable> serializables = new ArrayList<>(Arrays.asList(ids));
            this.providerService.removeByIds(serializables);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
    /**
     * 查询所有可用的供应商
     */
    @RequestMapping("/loadAllProviderName")
    public DataGridView loadAllProviderName(){
        QueryWrapper<Provider> wrapper = new QueryWrapper<>();
        wrapper.eq("available", Constant.AVAILABLE_TRUE);
        List<Provider> list = this.providerService.list(wrapper);
        return new DataGridView(list);
    }
}

